package main

import (
	"BJXM_Shooting/conf"
	"BJXM_Shooting/db"
	"BJXM_Shooting/game"
	"BJXM_Shooting/gate"
	"BJXM_Shooting/login"
	"fmt"
	"github.com/name5566/leaf"
	lconf "github.com/name5566/leaf/conf"
	"github.com/name5566/leaf/log"
	"io/ioutil"
	"os"
)

func main() {
	runArgs()

	var err error
	conf.CustomConfig, err = conf.ReadProjectConfig("conf/project.json")
	if err != nil {
		log.Fatal("读取配置错误:%v", err)
	} else {
		log.Debug("读取配置成功")
	}

	lconf.LogLevel = conf.Server.LogLevel
	lconf.LogPath = conf.Server.LogPath
	lconf.LogFlag = conf.LogFlag
	lconf.ConsolePort = conf.Server.ConsolePort
	lconf.ProfilePath = conf.Server.ProfilePath

	leaf.Run(
		db.Module,
		game.Module,
		gate.Module,
		login.Module,
	)

}

// 当参数运行时
func runArgs() {
	// 默认就有一个参数,跳过
	if len(os.Args) == 1 {
		return
	}

	// -p生成项目配置文件样本
	switch os.Args[1] {
	case "-p":
		byteJSON := conf.WriteProjectConfigExample()
		err := ioutil.WriteFile("conf/project_example.json", byteJSON, os.ModePerm)
		if err != nil {
			log.Fatal("保存配置文件失败:%v", err)
		} else {
			fmt.Println("保存成功:conf/project_example.json")
		}
	}
	os.Exit(0)
}
